<template>
	<view>
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">修改密码</block>
		</cu-custom>
		<view class="flex justify-center margin-top-lg">
			<image style="width: 150px; height: 150px; background-color: #eeeeee;" mode="aspectFit" src="/static/img/logo.png"></image>
		</view>
		<view>
			<view class="cu-form-group margin-top-lg">
				<view class="title">手机号码</view>
				<input placeholder="输入手机号" name="input" type="number" v-model="phone"></input>
				<view class="cu-capsule radius">
					<view class='cu-tag bg-blue '>
						+86
					</view>
					<view class="cu-tag line-blue">
						中国大陆
					</view>
				</view>
			</view>		
			<view class="cu-form-group">
				<view class="title">验证码</view>
				<input placeholder="输入验证码" name="input" type="number" v-model="smscode"></input>
				<button class='cu-btn bg-green shadow' @click="getSms">
					<view v-if="smsLoadingStatus==0" class="text-white">验证码</view>
					<view v-else-if="smsLoadingStatus==1"><text class="cuIcon-loading2 cuIconfont-spin"></text>加载</view>
					<view v-else-if="smsLoadingStatus==2" class="text-white">剩{{waitSeconds}}秒</view>
				</button>
			</view>
			<view class="cu-form-group">
				<view class="title">新密码</view>
				<input placeholder="输入密码(6-16)" name="input" type="password" v-model="password"></input>
			</view>
		</view>
		<view class="btn-row">
			<button type="primary" class="primary" @tap="fixpass">修改密码</button>
		</view>
	</view>
</template>

<script>
	var _self
	import service from '../../service.js';
	import mInput from '../../components/m-input.vue';

	export default {
		components: {
			mInput
		},
		data() {
			return {
				phone: '',
				password: '',			
				smscode: '',
				smsLoadingStatus: 0,
				timerId: null,
				waitSeconds: 300
			}
		},
		onLoad() {
			_self = this
		},
		methods: {
			getSms() {
				if (_self.timerId == null && _self.assertLengthGt(_self.phone, 11, '手机号码不合法')) {
					this.smsLoadingStatus = 1
					uni.request({
						url: this.api + '/users/smscode?phone=' + this.phone,
						method: 'GET',
						success: res => {
							console.log('get sms resps')
							console.log(res)
							if (_self.ifElseReturn(res)) {
								console.log('has send sms')
							}
						},
						complete: () => {
							_self.smsLoadingStatus = 2
							_self.timerId = setInterval(() => {
								if (_self.waitSeconds == 0) {
									_self.smsLoadingStatus = 0
									clearInterval(this.timerId);
									this.timerId = null;
									_self.waitSeconds = 300
								} else {
									_self.waitSeconds--;
								}
							}, 1000);
						}
					});
				}
			},
			fixpass() {			
				if (_self.assertLengthGt(_self.phone, 11, '手机号码不合法') &&
					_self.assertLengthGt(_self.password, 6, '密码最短为 6 个字符') &&				
					_self.assertLengthGt(_self.smscode, 6, '验证码不合法')) {

					const data = {
						phone: this.phone,
						password: this.password,						
						smscode: this.smscode
					}


					uni.request({
						url: this.api + '/users/repasswd',
						method: 'PUT',
						data: data,
						success: res => {
							if(_self.ifElseReturn(res)){
								this.cacheAccount(data.phone)
								uni.showToast({
									title: '修改成功'
								});
							}							
						},
						fail: () => {
							uni.showToast({
								title: '修改失败'
							});
						},
						complete: () => {
							uni.navigateBack({
								delta: 2
							});
						}
					});
				}

				// service.addUser(data);               

			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex: 1;
		flex-direction: column;
	}

	button.primary {
		background-color: #0faeff;
	}
</style>
